package com.gzsj.genealogyManagement.people.utils;

public class NumberToChinese {

    private static final char[] chineseNumerals = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
    private static final String[] units = {"", "十", "百", "千", "万", "十", "百", "千", "亿"};

    // 将对应的数字、转化为对应的汉字
    public static String numberToChinese(int number) {

        if (number == 0) {
            return "零";
        }

        String numStr = Integer.toString(number);
        StringBuilder result = new StringBuilder();

        int len = numStr.length();
        for (int i = 0; i < len; i++) {
            int num = numStr.charAt(i) - '0';
            if (num != 0) {
                result.append(chineseNumerals[num]).append(units[len - 1 - i]);
            } else {
                // 处理连续的零
                if (!result.toString().endsWith("零") && (len - 1 - i) % 4 != 0) {
                    result.append("零");
                }
            }
        }

        // 处理结尾的零和万、亿等单位后的零
        String finalResult = result.toString().replaceAll("零万", "万").replaceAll("零亿", "亿");
        if (finalResult.endsWith("零")) {
            finalResult = finalResult.substring(0, finalResult.length() - 1);
        }

        return finalResult;
    }

}
